/*
 * ViaClasses.cpp
 *
 *  Created on: Aug 22, 2013
 *      Author: root
 */

#include "model/clases/ViaClasses.h"
#include <iostream>
#include <string.h>
#include <sstream>
#include "model/Utils.h"
#include <typeinfo>

ViaClasses::ViaClasses(unsigned exit, const std::string & name, int orientation) : ComponentClasses()
{
	nombre = name;
	m_exitNumber = exit;
	initializeConnectors();
	tipoDeRelacionEntreClases = TipoDeRelacionEntreClasesComposicion;
}


ViaClasses::ViaClasses(const ViaClasses & otro) : ComponentClasses(otro) {
	for (std::list<Point*>::const_iterator it = otro.m_points.begin(); it != otro.m_points.end(); ++it)
		addPointAtEnd(new Point(*(*it)));
	m_exitNumber = otro.getExitNumber();
	initializeConnectors();
	tipoDeRelacionEntreClases = otro.tipoDeRelacionEntreClases;
}

ViaClasses::~ViaClasses(){
	std::list< Point* >::iterator it;
	for ( it = m_points.begin() ; it !=m_points.end() ; ++it )
		delete (*it);
}

ViaClasses& ViaClasses::operator=(const ViaClasses& otro) {
	ComponentClasses::operator=(otro);
	m_exitNumber = otro.getExitNumber();
	tipoDeRelacionEntreClases = otro.tipoDeRelacionEntreClases;
	initializeConnectors();

	return *this;
}


void ViaClasses::initializeConnectors() {
	componentType = ComponentTypeClasesVia;
}

void ViaClasses::serialize(XmlWriter & writer_rep, XmlWriter & writer_comp)
{

	Utils utils;
	writer_rep.addRootChild(TARGET_VIA, "");
	ComponentClasses::serialize(writer_rep, writer_comp);
	writer_rep.addCurrentNodeProperty("nroSalida", utils.convertToString(m_exitNumber).c_str());
	writer_rep.addCurrentNodeProperty("relationType", utils.convertToString(tipoDeRelacionEntreClases).c_str());
	std::list< Point* >::iterator it;
	xmlNode* nodoActual;
	for ( it = m_points.begin() ; it != m_points.end() ; ++it) {
		nodoActual = writer_rep.addCurrentNodeChild(TARGET_PUNTO, "");
		writer_rep.addProperty( nodoActual, TARGET_POS_X,utils.convertToString((*it)->getX()).c_str());
		writer_rep.addProperty( nodoActual, TARGET_POS_Y,utils.convertToString((*it)->getY()).c_str());
		writer_rep.addProperty( nodoActual, "movible",utils.convertToString((*it)->isMobile()).c_str());
	}

}


void ViaClasses::deserialize(XmlReader & reader)
{

	Utils utils;
	ComponentClasses::deserialize(reader);
	m_exitNumber = utils.convertToUnsigned(reader.getCurrentNodeProperty("nroSalida"));

	xmlNode* nodoVia = reader.getCurrentNode();
	reader.setCurrentNode(reader.getCurrentNodeChild());
	while(reader.getCurrentNode()){
		const char* nameNodo = reader.getCurrentNodeName();
        if ( strcmp(nameNodo, TARGET_PUNTO) == 0 )
        {
        	//se encontro un nodo punto
        	int x = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_POS_X));
        	int y = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_POS_Y));
        	bool movible = utils.convertToUnsigned(reader.getCurrentNodeProperty("movible"));
        	Point* point = new Point(x,y, movible);
        	addPointAtEnd(point);
        }
        reader.setCurrentNode(reader.nextNode());
	}
	reader.setCurrentNode(nodoVia);
}

void ViaClasses::deserialize(XmlReader & reader, XmlReader & readerModel)
{
	Utils utils;
	ComponentClasses::deserialize(reader);
	m_exitNumber = utils.convertToUnsigned(reader.getCurrentNodeProperty("nroSalida"));
	std::string tipo = reader.getCurrentNodeProperty("relationType");
	TipoDeRelacionEntreClases rel = getTipoRelacion(utils.convertToInt(tipo));
	this->tipoDeRelacionEntreClases = rel;

	xmlNode* nodoVia = reader.getCurrentNode();
	reader.setCurrentNode(reader.getCurrentNodeChild());
	while ( reader.getCurrentNode() )
	{
		const char* nameNodo = reader.getCurrentNodeName();
        if ( strcmp(nameNodo, TARGET_PUNTO) == 0 )
        {
        	//se encontro un nodo punto
        	int x = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_POS_X));
        	int y = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_POS_Y));
        	bool movible = utils.convertToUnsigned(reader.getCurrentNodeProperty("movible"));
        	Point* point = new Point(x,y, movible);
        	addPointAtEnd(point);
        }
        reader.setCurrentNode(reader.nextNode());
	}
	reader.setCurrentNode(nodoVia);
}
